/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : WordHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 12, 2008       Raghu Sarraff			       Created
 */
package lwf.handler.english;

import java.util.List;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.util.ImageHtml;


/**
 * Type the class description here
 *  
 */
public class WordHandler {
	
	private String displayData;
	private String formattedDisplay;
	private boolean showPrevious = false;
	private boolean showNext = false;
	private boolean showbuttontop = false;
	
	public WordHandler() {
		//setDisplayBlock();
		String a = "";
	}
	
	/**
	 * @return Returns the showNext.
	 */
	public boolean isShowNext() {
		return showNext;
	}
	/**
	 * @param showNext The showNext to set.
	 */
	public void setShowNext(boolean showNext) {
		this.showNext = showNext;
	}
	/**
	 * @return Returns the showPrevious.
	 */
	public boolean isShowPrevious() {
		return showPrevious;
	}
	/**
	 * @param showPrevious The showPrevious to set.
	 */
	public void setShowPrevious(boolean showPrevious) {
		this.showPrevious = showPrevious;
	}
	
	
	
	/**
	 * @param showbuttontop The showbuttontop to set.
	 */
	public void setShowbuttontop(boolean showbuttontop) {
		this.showbuttontop = showbuttontop;
	}
	/**
	 * @return Returns the displayData.
	 */
	public String getDisplayData() {
		return displayData;
	}
	/**
	 * @param displayData The displayData to set.
	 */
	public void setDisplayData(String displayData) {
		this.displayData = displayData;
	}
	
	public void setDisplayData(int index, int limit, String data) {
		
		if(index >= 0 && (limit-1-index > 0)) {
			setShowNext(true);
		}
		
		if(index > 0) {
			setShowPrevious(true);
		}
		
    	StringBuffer sbWData = new StringBuffer();
    	sbWData.append("<table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
			sbWData.append("<tr>");
    			sbWData.append("<td align=\"center\">");
    			sbWData.append(processRowData(data));
				sbWData.append("</td>");
			sbWData.append("</tr>");
			sbWData.append("<tr>");
				sbWData.append("<td ><hr></td>");
        	sbWData.append("</tr>");
        sbWData.append("</table>");    		
            
        setDisplayData(sbWData.toString());
	}
	
	
    public void setDisplayData(int start, int end, List lstData) {
    	int nDataSize = 0;
    	StringBuffer sbWData = new StringBuffer();
    	if(lstData != null) {
        	nDataSize = lstData.size();
        	sbWData.append("<table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
        	for(int i=start; i<end; i++) {
        		if(i<nDataSize) {
        			sbWData.append("<tr>");
    	    			sbWData.append("<td>");
    	    			sbWData.append(processRowData(lstData.get(i)));
    					sbWData.append("</td>");
    				sbWData.append("</tr>");
        			sbWData.append("<tr>");
    					sbWData.append("<td ><hr></td>");
    	        	sbWData.append("</tr>");
        		}
        		else {
        			break;
        		}
        	}
            sbWData.append("</table>");    		
    	}
        //Set show next button
        int datarows = end - start;
        if(start - datarows >= 0) {
        	setShowPrevious(true);
        }
        if(nDataSize - end > 0) {
        	setShowNext(true);
        }
        setDisplayData(sbWData.toString());
    }

    //This method in the child class needs to be overloaded
    //if the data needs to be formatted....
    protected String processRowData(Object obj) {
    	return (String) obj;
    }
	
	/**
	 * @return Returns the formattedDisplay.
	 */
	public String getFormattedDisplay() {
		formatDisplay();
		return formattedDisplay;
	}
	/**
	 * This format holds the 
	 * 1. Question number
	 * 2. Question information. May contain OK button (upto the process handler).
	 * 3. Display the NEXT button.
	 * 
	 *  |------------|--------------------------------------------|-------------|--------|
	 *  |            |                                            |             |        |
	 *  |            |          Display Information               |             |        |
	 *  |  Previous  |                                            | NEXT button |        |
	 *  |------------|--------------------------------------------|-------------|--------|
	 */
	protected void formatDisplay() {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table align=\"center\" width=\"100%\" border=\"0\" >");
		    sbQBlock.append("<tr>");
		        if(showPrevious) {
		        	if(this.showbuttontop) {
		        		sbQBlock.append("<td valign=\"top\" align=\"left\" width=\"15%\">");
		        	}
		        	else {
		        		sbQBlock.append("<td valign=\"bottom\" align=\"left\" width=\"15%\">");	
		        	}
		        	sbQBlock.append(ImageHtml.getButton(IHandlerConstant.PREVIOUS, "processDisplay('previous')"));
	        		sbQBlock.append("</td>");
		        }
		        else {
		        	sbQBlock.append("<td width=\"15%\"> &nbsp; </td>");		        	
		        }
		    	sbQBlock.append("<td width=\"60%\" align=\"center\" nowrap>");
		    		sbQBlock.append(getDisplayData());
		        sbQBlock.append("</td>");
		        if(showNext) {
		        	if(this.showbuttontop) {
		        		sbQBlock.append("<td valign=\"top\" align=\"left\" width=\"15%\">");
		        	}
		        	else {
		        		sbQBlock.append("<td valign=\"bottom\" align=\"left\" width=\"15%\">");	
		        	}
		        	    sbQBlock.append(ImageHtml.getButton(IHandlerConstant.NEXT, "processDisplay('next')"));
	        		sbQBlock.append("</td>");
		        }
		        else {
		        	sbQBlock.append("<td width=\"15%\"> &nbsp; </td>");		        	
		        }
		        sbQBlock.append("<td width=\"10%\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
	    sbQBlock.append("</table>");		
	    this.formattedDisplay = sbQBlock.toString();
	}
	/*
    protected String getWordData(String word) {
    	String image = ImageHtml.getWordImage(word);
		StringBuffer sbWData = new StringBuffer();
		sbWData.append("<table   width=\"100%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
			sbWData.append("<tr>");
			    if(image.length() != 0) {
					sbWData.append("<td   width=\"40%\" class=\"dataleft\">");
						sbWData.append(word);
					sbWData.append("</td>");
					sbWData.append("<td  width=\"10%\"> &nbsp;</td>");
					sbWData.append("<td  width=\"50%\">");
						sbWData.append(image);
					sbWData.append("</td>");
			    }
			    else {
					sbWData.append("<td class=\"dataleft\" colspan=\"3\" >");
						sbWData.append(word);
					sbWData.append("</td>");
			    }
			sbWData.append("</tr>");
		sbWData.append("</table>");

		return sbWData.toString();
    }
    */
}

